home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 722 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. From: adamnash@cs.stanford.edu (Adam Nash)
  2. Message-ID: <adamnash-1303962301470001@gel.stanford.edu>
  3. X-Original-Date: 14 Mar 1996 07:02:58 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 15 Mar 96 00:12:05 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Problem with new in templates...
  9. Organization: Computer Science, Stanford University
  10. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  11.     iQBFAgUBMUi4E+EDnX0m9pzZAQHhzgF+I0aZTe0bM0trjhNWDfBgKfGVWj4zwKE+
  12.     yIvGY0W4ore5GfNcokISH7pSMzQBQN/s
  13.     =105S
  14.  
  15. OK Problem Scenario:
  16.  
  17. There is a templatized data structure that is based on type DATA.  This
  18. data structure features a method that depends on a method in type DATA. 
  19. Thus, for semantic reasons, the template assumes that DATA is a pointer
  20. type.
  21.  
  22. So, here is the method:
  23.  
  24. template <class DATA>
  25. void GIDTable::ReadFromStream(LStream *inStream)
  26. {
  27.    DATA data;
  28.  
  29.    ...
  30.  
  31.    data = new DATA;
  32.    (*data).ReadFromStream(inStream);
  33.  
  34.    ...
  35. }
  36.  
  37.  
  38. The problem occurs on the second line.  data is of type DATA, so you
  39. really want to allocate a new (*DATA).  IE, if DATA is a CBar *, then data
  40. is a CBar *,
  41. so we want it to say: data = new CBar;
  42.  
  43. How can I do this, only knowing the pointer type?
  44. IE, is there a version of new that can take the pointer type, rather than
  45. the actual type?  RTTI is fair game.
  46.  
  47.  
  48. Please Help, this is an actual problem!
  49.  
  50.       -Adam
  51.  
  52. -- 
  53. Adam Nash
  54. CS 108 TA
  55. adamnash@cs.stanford.edu
  56. http://www-leland.stanford.edu/~smashman
  57. Stanford University - Computer Science
  58. ---
  59. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  60. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  61. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  62. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  63. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  64.